package com.theme.passport.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.constant.CarouselTypeEnum;
import com.theme.passport.entity.theme.CarouselEntity;
import com.theme.passport.entity.theme.ThemeEntity;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.service.theme.CarouselService;
import com.theme.passport.service.theme.ThemeService;
import com.theme.passport.service.theme.WallpaperService;
import com.theme.passport.service.theme.WidgeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 轮播图
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-12-08 15:14:01
 */
@RestController
@RequestMapping("sys/carousel")
public class SysCarouselController {
    @Autowired
    private CarouselService tabCarouselService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private WidgeService widgeService;

    @Autowired
    private WallpaperService wallpaperService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:carousel:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tabCarouselService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:carousel:info")
    public R info(@PathVariable("id") Long id){
		CarouselEntity tabCarousel = tabCarouselService.getById(id);

        return R.ok().put("carousel", tabCarousel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:carousel:save")
    public R save(@RequestBody CarouselEntity tabCarousel){
        if (tabCarousel.equals(CarouselTypeEnum.THEME.getValue())){
            //1.判断主题名称是否已经存在
            QueryWrapper<ThemeEntity> nameWrapper = new QueryWrapper<>();
            nameWrapper.and(wrapper -> wrapper
                    .eq("type", tabCarousel.getType())
                    .eq("name", tabCarousel.getObjectName()));
            List<ThemeEntity> nameList = themeService.list(nameWrapper);
            if (nameList == null || nameList.size() <=0 ){
                return R.error("该主题名称不存在,请确认该主题是否存在!");
            }
            ThemeEntity themeEntity = nameList.get(0);
            tabCarousel.setClassifyId(themeEntity.getClassifyId());
            tabCarousel.setClassifyName(themeEntity.getClassifyName());
            tabCarousel.setType(tabCarousel.getType());
            tabCarousel.setObjectId(themeEntity.getId());
            tabCarousel.setObjectName(themeEntity.getName());
            tabCarouselService.save(tabCarousel);
            return R.ok();
        }
        if (tabCarousel.equals(CarouselTypeEnum.WIDGE.getValue())){
            //1.判断主题名称是否已经存在
            QueryWrapper<WidgeEntity> nameWrapper = new QueryWrapper<>();
            nameWrapper.and(wrapper -> wrapper
                    .eq("type", tabCarousel.getType())
                    .eq("name", tabCarousel.getObjectName()));
            List<WidgeEntity> nameList = widgeService.list(nameWrapper);
            if (nameList == null || nameList.size() <=0 ){
                return R.error("该组件名称不存在,请确认该组件是否存在!");
            }
            WidgeEntity widgeEntity = nameList.get(0);
            tabCarousel.setClassifyId(widgeEntity.getClassifyId());
            tabCarousel.setClassifyName(widgeEntity.getClassifyName());
            tabCarousel.setType(tabCarousel.getType());
            tabCarousel.setObjectId(widgeEntity.getId());
            tabCarousel.setObjectName(widgeEntity.getName());
            tabCarouselService.save(tabCarousel);
            return R.ok();
        }

        if (tabCarousel.equals(CarouselTypeEnum.WALLPAPER.getValue())){
            //1.判断主题名称是否已经存在
            QueryWrapper<WallpaperEntity> nameWrapper = new QueryWrapper<>();
            nameWrapper.and(wrapper -> wrapper
                    .eq("type", tabCarousel.getType())
                    .eq("name", tabCarousel.getObjectName()));
            List<WallpaperEntity> nameList = wallpaperService.list(nameWrapper);
            if (nameList == null || nameList.size() <=0 ){
                return R.error("该壁纸名称不存在,请确认该壁纸是否存在!");
            }
            WallpaperEntity wallpaperEntity = nameList.get(0);
            tabCarousel.setClassifyId(wallpaperEntity.getClassifyId());
            tabCarousel.setClassifyName(wallpaperEntity.getClassifyName());
            tabCarousel.setType(tabCarousel.getType());
            tabCarousel.setObjectId(wallpaperEntity.getId());
            tabCarousel.setObjectName(wallpaperEntity.getName());
            tabCarouselService.save(tabCarousel);
            return R.ok();
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:carousel:update")
    public R update(@RequestBody CarouselEntity tabCarousel){
        if (tabCarousel.equals(CarouselTypeEnum.THEME.getValue())){
            //1.判断主题名称是否已经存在
            QueryWrapper<ThemeEntity> nameWrapper = new QueryWrapper<>();
            nameWrapper.and(wrapper -> wrapper
                    .eq("type", tabCarousel.getType())
                    .eq("name", tabCarousel.getObjectName()));
            List<ThemeEntity> nameList = themeService.list(nameWrapper);
            if (nameList == null || nameList.size() <=0 ){
                return R.error("该主题名称不存在,请确认该主题是否存在!");
            }
            ThemeEntity themeEntity = nameList.get(0);
            tabCarousel.setClassifyId(themeEntity.getClassifyId());
            tabCarousel.setClassifyName(themeEntity.getClassifyName());
            tabCarousel.setType(tabCarousel.getType());
            tabCarousel.setObjectId(themeEntity.getId());
            tabCarousel.setObjectName(themeEntity.getName());
            tabCarouselService.updateById(tabCarousel);
            return R.ok();
        }
        if (tabCarousel.equals(CarouselTypeEnum.WIDGE.getValue())){
            //1.判断主题名称是否已经存在
            QueryWrapper<WidgeEntity> nameWrapper = new QueryWrapper<>();
            nameWrapper.and(wrapper -> wrapper
                    .eq("type", tabCarousel.getType())
                    .eq("name", tabCarousel.getObjectName()));
            List<WidgeEntity> nameList = widgeService.list(nameWrapper);
            if (nameList == null || nameList.size() <=0 ){
                return R.error("该组件名称不存在,请确认该组件是否存在!");
            }
            WidgeEntity widgeEntity = nameList.get(0);
            tabCarousel.setClassifyId(widgeEntity.getClassifyId());
            tabCarousel.setClassifyName(widgeEntity.getClassifyName());
            tabCarousel.setType(tabCarousel.getType());
            tabCarousel.setObjectId(widgeEntity.getId());
            tabCarousel.setObjectName(widgeEntity.getName());
            tabCarouselService.updateById(tabCarousel);
            return R.ok();
        }
        if (tabCarousel.equals(CarouselTypeEnum. WALLPAPER.getValue())){
            //1.判断主题名称是否已经存在
            QueryWrapper<WallpaperEntity> nameWrapper = new QueryWrapper<>();
            nameWrapper.and(wrapper -> wrapper
                    .eq("type", tabCarousel.getType())
                    .eq("name", tabCarousel.getObjectName()));
            List<WallpaperEntity> nameList = wallpaperService.list(nameWrapper);
            if (nameList == null || nameList.size() <=0 ){
                return R.error("该壁纸名称不存在,请确认该壁纸是否存在!");
            }
            WallpaperEntity wallpaperEntity = nameList.get(0);
            tabCarousel.setClassifyId(wallpaperEntity.getClassifyId());
            tabCarousel.setClassifyName(wallpaperEntity.getClassifyName());
            tabCarousel.setType(tabCarousel.getType());
            tabCarousel.setObjectId(wallpaperEntity.getId());
            tabCarousel.setObjectName(wallpaperEntity.getName());
            tabCarouselService.updateById(tabCarousel);
            return R.ok();
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:carousel:delete")
    public R delete(@RequestBody Long[] ids){
		tabCarouselService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
